package com.charactereducation.admin.controller.ckeditor;

import com.charactereducation.webcore.controller.BaseController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by Baggio on 2019-3-1.
 */
@RequestMapping("/admin/ckeditor")
@RestController
public class CkeditorContorller extends BaseController {

    /**
     * 获取验证码
     */
    @ResponseBody
    @RequestMapping("/upload")
        //名字upload是固定的，有兴趣，可以打开浏览器查看元素验证
    public String upload(@RequestParam("upload") MultipartFile file, String CKEditorFuncNum) throws Exception {
            logger.info("上传图片处理"+CKEditorFuncNum);

        // 获取文件名
        String fileName = file.getOriginalFilename();
        logger.info("fileName===>>"+fileName);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //实际处理肯定是要加上一段唯一的字符串（如现在时间），这里简单加 cun
        String newFileName = "/img/weChat/esc.png";
        logger.info("newFileName===>>"+newFileName);
        //使用架包 common-io实现图片上传
     //   FileUtils.copyInputStreamToFile(file.getInputStream(), new File(imageFilePath + newFileName));
        //实现图片回显，基本上是固定代码，只需改路劲即可
        StringBuffer sb = new StringBuffer();
        sb.append("<script type=\"text/javascript\">");
        sb.append("window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ",'" +  newFileName
                + "','')");
        sb.append("</script>");

        return sb.toString();
    }



}


